<?php

namespace App\Console\Commands;

use App\User;
use Illuminate\Console\Command;
use App\Models\CourseClass;
use App\Models\Studying;
use App\Events\WxMessagePush;
use App\Contant\WxMessageType;
use App\Events\WxCustomerMessagePush;
class CreateCourseLiveNotice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:course_live_notice {id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '直播课开课提醒';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $class_id = $this->argument('id');
        $courseClass = CourseClass::where('id',$class_id)->select('id','title','user_id','explain_url')->first();

        $author  = $courseClass->author;

//        $fans = Studying::where('course_class_id',$class_id)->where('id','')->get();

        $studying = Studying::where('course_class_id',$class_id)->get();
        $data['type'] = WxMessageType::IMAGE;
        $author = $courseClass->author;

        $title = "【直播提醒】健美冠军的健身秘籍";
        $desc = $author->name."[导师系列课]"."\n"."该直播课第二期【运动营养基础】还有30分钟就开始了，课程无回看，不要错过~";
        $data['list'] = [[
            "title"=>$title,
            "description"=>$desc,
            "url"=>env('PT_WEB_URL').'/course/detail?id='.$class_id,
            "picurl"=>'http://image.saipubbs.com/'.$courseClass->explain_url]
        ];
//        dd($data,$studying);
//        if($courseClass->push_message){
        foreach($studying as $study){
            $user = User::where('id',$study->user_id)->select('openid','name')->first();

            if($user && $user->openid){
                $data['openid'] = $user->openid;
                event(new WxCustomerMessagePush($data));
            }
        }
//        }
    }
}
